#define DEBUG
#include <cstdio>

using namespace std;

const int maxr=75, maxc=75;

int getSum(int suf[][maxc+1], int x1, int y1, int x2, int y2) {
  return suf[x2][y2]-suf[x1][y2]-suf[x2][y1]+suf[x1][y1];
}

bool solve(int x, int r, int c, int suf[][maxc+1]) {
  for (int i=1; i<=r; i++) {
    if (suf[i][c]>=4*x) {
      for (int j=i+1; j<=r; j++) {
	if (suf[j][c]-suf[i][c]>=4*x) {
	  for (int k=j+1; k<=r; k++) {
	    if (suf[k][c]-suf[j][c]>=4*x && suf[r][c]-suf[k][c]>=4*x) {
	      int p, q=0;
	      for (p=q++; q<=c && (getSum(suf, 0, p, i, q)<x || getSum(suf, i, p, j, q)<x || getSum(suf, j, p, k, q)<x || getSum(suf, k, p, r, q)<x); q++);
	      if (q>c) {
		continue;
	      }
	      for (p=q++; q<=c && (getSum(suf, 0, p, i, q)<x || getSum(suf, i, p, j, q)<x || getSum(suf, j, p, k, q)<x || getSum(suf, k, p, r, q)<x); q++);
	      if (q>c) {
		continue;
	      }
	      for (p=q++; q<=c && (getSum(suf, 0, p, i, q)<x || getSum(suf, i, p, j, q)<x || getSum(suf, j, p, k, q)<x || getSum(suf, k, p, r, q)<x); q++);
	      if (q>c) {
		continue;
	      }
	      if (getSum(suf, 0, q, i, c)>=x && getSum(suf, i, q, j, c)>=x && getSum(suf, j, q, k, c)>=x && getSum(suf, k, q, r, c)>=x) {
		return true;
	      }
	    }
	  }
	}
      }
    }
  }
  return false;
}

int main() {
#ifdef DEBUG
  freopen("src.in", "r", stdin);
  freopen("src.out", "w", stdout);
#endif

  static int suf[maxr+1][maxc+1];
  int r, c;
  scanf("%d %d", &r, &c);
  for (int i=1; i<=r; i++) {
    for (int j=1; j<=c; j++) {
      int t;
      scanf("%1d", &t);
      suf[i][j] = suf[i-1][j]+suf[i][j-1]-suf[i-1][j-1]+t;
    }
  }

  int ans=0;
  for (int x=0, y=suf[r][c], mid; x<=y; ) {
    mid = (x+y)/2;
    if (solve(mid, r, c, suf)) {
      x = mid+1;
      ans = mid;
    } else {
      y = mid-1;
    }
  }

  printf("%d", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
